/**
 * Copyright (c) 2022 Beijing Jiaotong University
 * PhotLab is licensed under [Open Source License].
 * You can use this software according to the terms and conditions of the [Open Source License].
 * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *
 * See the [Open Source License] for more details.
 *
 * Author: Qing'ao Zhang, Jiang Long, Zhenjie Wei
 * Created: Mar. 15, 2023
 * Supported by: National Key Research and Development Program of China
 */

import { PhotFile } from "../thrift/gen-nodejs/file_data_type_types"
import { ProjectConfig, ProjectType } from "../thrift/gen-nodejs/project_data_type_types"
import thriftClient from "../utils/conn"
import service from "../thrift/gen-nodejs/Project"

export default class ProjectService {
    private _client: any
    constructor(socket: [string, number]) {
        this._client = thriftClient(service, socket[0], socket[1])
    }

    /**
	 * Open a folder.
	 * @param directoryPath - Folder Path
	 * @returns list<PhotFile>: File List
	 */
    public async openDirectory(directoryPath: string): Promise<PhotFile[]> {
        return await this._client.open_directory(directoryPath)
    }

    /**
	 * Open file.
	 * @param filePath - File path
	 * @returns PhotFile: File object
	 */
    public async openFile(filePath: string): Promise<PhotFile> {
        return await this._client.open_file(filePath)
    }

    /**
	 * Create a new simulation project.
	 * @param projectPath - Project Path
	 * @param projectName - Project Name
	 * @param projectType - Project Type
	 * @returns ProjectConfig: Project configuration information.
	 */
    public async newProject(projectPath: string, projectName: string, projectType: ProjectType): Promise<ProjectConfig> {
        return await this._client.new_project(projectPath, projectName, projectType)
    }

    /**
	 * Open a simulation project. The server loads and saves the configuration of the simulation project.
	 * @param projectPath - Project Path
	 * @returns ProjectConfig: Project configuration information.
	 */
    public async openProject(projectPath: string): Promise<ProjectConfig> {
        return await this._client.open_project(projectPath)
    }

    /**
	 * Close the simulation project. The server will save and delete the relevant simulation project configurations.
	 * @param projectName - Project Name
	 * @param windowConfig - Front-end window configuration
	 * @returns null
	 */
    public async closeProject(projectName: string, projectConfig: string) {
        return await this._client.close_project(projectName, projectConfig)
    }

    /**
	 * Refresh simulation project configuration: When the files of a simulation project are updated, 
	 * it is necessary to refresh the simulation project configuration.
	 * @param projectId - Project Name
	 * @returns ProjectConfig: Project configuration information.
	 */
	public async refreshProject (projectId: string): Promise<ProjectConfig> {
		return await this._client.refresh_project(projectId)
	}

    /**
	 * New Folder
	 * @param directoryPath - Folder Path
	 * @returns null
	 */
    public async newDirectory(directoryPath: string) {
        return await this._client.new_directory(directoryPath)
    }

    /**
	 * New File
	 * @param filePath - File Path
	 * @param fileObject File Object
	 * @returns null
	 */
    public async newFile(filePath: string, fileObject: PhotFile) {
        return await this._client.new_file(filePath, fileObject)
    }

    /**
	 * Delete files or folders.
	 * @param path - File or folder path
	 * @returns null
	 */
    public async deleteFile(filePath: string) {
        return await this._client.delete_file(filePath)
    }

    /**
	 * Save File
	 * @param filePath - File path
	 * @param fileObject - File object
	 * @returns null
	 */
    public async saveFile(filePath: string, fileObject: PhotFile) {
        return await this._client.save_file(filePath, fileObject)
    }
}
